/**
 * 所有的数据库的访问方法
 */
let { connection } = require('./init');
/**
 * 登录
 */
function login(username, password, callback) {
    let sql = `select * from user where username='${username}' and password='${password}'`;
    connection.query(sql, function (error, results, fields) {
        if (error) {
            callback(error);
        } else {
            callback(null, results);
        }
    })
}
/**
 * 查询是否被注册
 */
function isuser(username, callback) {
    let sql = `select * from user where username='${username}'`;
    connection.query(sql, function (error, results, fields) {
        if (error) {
            callback(error);
        } else {
            if (results.length) {
                callback(null, {
                    code: 0
                });
            } else {
                callback(null, {
                    code: 1
                });
            }
        }
    })
}
/**
 * 注册
 */
function register(data, callback) {
    let sql = `insert into user(username,password,email) values('${data.username}','${data.password}','${data.email}')`;
    console.log(sql)
    connection.query(sql, function (error, results, fields) {
        if (error) {
            callback(error);
        } else {
            callback(null, results);
        }
    })
}
/**
 * 产品查询
 */
function commoditybyid(id, callback) {
    let sql = `select * from commodity where id=${id}`;
    connection.query(sql, function (error, results, fields) {
        if (error) {
            callback(error);
        } else {
            callback(null, results);
        }
    })
}
/**
 *显示所有产品
 */
function commodityall(callback) {
    let sql = `select * from commodity`;
    connection.query(sql, function (error, results, fields) {
        if (error) {
            callback(error);
        } else {
            callback(null, results);
        }
    })
}
/**
 *显示所有轮播图
 */
function swiperall(callback) {
    let sql = `select * from swiper`;
    connection.query(sql, function (error, results, fields) {
        if (error) {
            callback(error);
        } else {
            callback(null, results);
        }
    })
}
/**
 * 根据分类显示所有的产品
 */
function commoditybytype(type) {
    let sql = `select * from commodity where type='${type}'`;
    connection.query(sql, function (error, results, fields) {
        if (error) {
            callback(error);
        } else {
            callback(null, results);
        }
    })
}
/**
 * 根据名字显示分类
 */
function commoditybyname(name, callback) {
    let sql = `select * from commodity where name like '%${name}%'`;
    connection.query(sql, function (error, results, fields) {
        if (error) {
            callback(error);
        } else {
            callback(null, results);
        }
    })
}
/**
 * 订单提交
 */
function addshopcardinfo(data, callback) {
    let sql = `insert into card(username,comm) values('${data.username}','${data.comm}')`;
    console.log(sql)
    connection.query(sql, function (error, results, fields) {
        if (error) {
            callback(error);
        } else {
            callback(null, results);
        }
    })
}
/**
 * 显示所有订单
 */
function showcardbyallbyname(name, callback) {
    let sql = `select * from card where username='${name}'`;
    connection.query(sql, function (error, results, fields) {
        if (error) {
            callback(error);
        } else {
            callback(null, results);
        }
    })
}
/**
 * 订单查询
 */
function showcardbyid() {

}
/**
 * 订单取消
 */
function delcardbyid(id, callback) {
    let sql = `DELETE FROM card WHERE id=${id}`;
    connection.query(sql, function (error, results, fields) {
        if (error) {
            callback(error);
        } else {
            callback(null, results);
        }
    })
}
/**
 * 订单修改
 */
function updatacardbyid() {

}

exports.login = login;
exports.register = register;
exports.isuser = isuser;
exports.commoditybyid = commoditybyid;
exports.commodityall = commodityall;
exports.commoditybytype = commoditybytype;
exports.addshopcardinfo = addshopcardinfo;
exports.showcardbyid = showcardbyid;
exports.delcardbyid = delcardbyid;
exports.updatacardbyid = updatacardbyid;
exports.commoditybyname = commoditybyname;
exports.showcardbyallbyname = showcardbyallbyname;
exports.swiperall=swiperall;